package cn.zyx.controller;


import cn.zyx.dao.PhoneDao;
import cn.zyx.entity.Phone;
import cn.zyx.service.PhoneService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * (Phone)表控制层
 *
 * @author makejava
 * @since 2021-12-11 18:49:59
 */
@CrossOrigin
@RestController
@RequestMapping("phone")
public class PhoneController {
    /**
     * 服务对象
     */
    @Resource
    private PhoneService phoneService;
    @Resource
    private PhoneDao phoneDao;
    @Resource
    private ReturnPayApi returnPayApi;



    /**
     * 根据openid 查询
     */
    @RequestMapping("selopenid")
    public List<Phone> selopenid(String openid){
        return phoneService.queryopenId(openid);
    }
    /**
     * 根据订单编号查询
     */
    @RequestMapping("selnumber")
    public List<Phone> selnumber(String number){
        return phoneService.querynumber(number);
    }

    @RequestMapping("add")
    public Phone add(@RequestBody Phone phone) {
        PhoneController phoneController = new PhoneController();
        String time = phoneController.time();
////        添加小程序用户标识
//        phone.setOpenid(phone.getOpenid());
////        添加电话号码
//        phone.setPhonename(phone.getPhonename());
////        添加原价金额
//        phone.setHmoney(phone.getHmoney());
////        添加付款金额
//        phone.setFmoney(phone.getFmoney());
////        添加运营商
//        phone.setOperator(phone.getOperator());
////        添加号码地区
//        phone.setHregion(phone.getHregion());
////        添加订单编号
//        phone.setNumber(phone.getNumber());
//        添加下单时间
        phone.setStart_date(time);
//        添加充值状态
        phone.setHstatus("1");
        phoneService.insert(phone);
        return phone;
    }

    /**
     * 通过运营商下拉框&地区名&充值金额下拉框&充值状态
     *
     * @param
     * @return
     */
    @RequestMapping("sel")
    public Map<String, Object> query(@RequestParam(defaultValue = "1") Integer page, Integer limit, String operator, String hregion, String hmoney, String hstatus,String start_date,String end_date) {
        List<Phone> phones = this.phoneService.queryAllByLimit((page - 1) * limit, limit, operator, hregion, hmoney, hstatus,start_date,end_date);
        int count = phoneDao.count(operator, hregion, hmoney, hstatus,start_date,end_date);
        Map<String, Object> map = new HashMap<>();
        map.put("success", true);
        map.put("data", phones);
        map.put("count", count);
        map.put("code", 0);
        return map;
    }

    /**
     * 将未充值用户进行充值
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("cupd")
    public Map<String, Object> cupd(Integer id) {
        Phone phone = new Phone();
        phone.setId(id);
        phone.setHstatus("2");
        PhoneController phoneController = new PhoneController();
        String time = phoneController.time();
        phone.setEnd_date(time);
        phoneService.update(phone);
        Map<String, Object> map = new HashMap<>();
        map.put("isOk", true);
        map.put("code", 0);
        return map;
    }

    /**
     * 将未充值用户进行退款
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("tupd")
    public Map<String, Object> tupd(Integer id, String orderNum, String money, String desc, HttpServletResponse response) {
        Phone phone1 = phoneService.queryById(id);
//        int a = (int) (Float.valueOf(phone1.getFmoney())*100);
//        returnPayApi.refund(phone1.getNumber(),"",String.valueOf(a), response);
//        Map<String, Object> refund = returnPayApi.refund(phone1.getNumber(), "", "1", response);
        Phone phone = new Phone();
        phone.setId(id);
        phone.setHstatus("3");
        PhoneController phoneController = new PhoneController();
        String time = phoneController.time();
        phone.setEnd_date(time);
        phone.setNumber(orderNum);
        phone.setFmoney(money);
        phone.setDesc(desc);
        phoneService.update(phone);
        Map<String, Object> map = new HashMap<>();
        map.put("success", true);
        map.put("code", 0);
        return map;
    }

    /**
     * 将充值完成或者退款成功记录进行删除
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("dupd")
    public Map<String, Object> dupd(Integer id) {
        Phone phone = new Phone();
        phone.setId(id);
        phone.setIsdel(1);
        phoneService.update(phone);
        Map<String, Object> map = new HashMap<>();
        map.put("isOk", true);
        map.put("code", 0);
        return map;
    }

    /**
     * 批量删除
     *
     * @param
     * @return
     */
    @RequestMapping("dupds")
    public Map<String, Object> dupds(String idsStr) {
        String[] split = idsStr.split(",");
        for (int i = 0; i < split.length; i++) {
            Phone phone = new Phone();
            String s = split[i];
            int i1 = Integer.parseInt(s);
            System.out.println(s + "--");
            phone.setId(i1);
            phone.setIsdel(1);
            phoneService.update(phone);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("isOk", true);
        map.put("code", 0);
        return map;
    }

    public String time() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        return format;
    }

    public static void main(String[] args) {
        String s = "1";
        int i1 = Integer.parseInt(s);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Phone selectOne(Integer id) {
        return this.phoneService.queryById(id);
    }

}